package net.huashitong.user.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.UserInfo;
import net.huashitong.Indicators.filter.UserInfoFilter;
import net.huashitong.Indicators.service.UserInfoService;
import net.huashitong.until.Until;
import net.huashitong.user.service.UserAuthService;
import net.huashitong.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/app/userauth")
public class UserAuthController extends JsonBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserAuthService authService;

    @Override
    protected String getView(String s) {
        return null;
    }

    Logger log = LoggerFactory.getLogger(UserController.class);

    /**
     * App是否需要安全验证
     * @param filter openId(必填)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ifappvalidate",method = RequestMethod.POST)
    public Message ifAPPValidate(@RequestBody UserInfoFilter filter){

        String sysId = filter.getOpenId();
        UserInfo userInfo = userInfoService.getById(sysId);
        String userId = "";
        //用户Id
        if(ObjectUtils.isNotBlank(userInfo.getUserId())){
            userId = userInfo.getUserId();
        }
        String url = "userid=" + userId;
        String ifAppValidate = null;
        try {
            ifAppValidate = Until.HttpGET(Until.IF_APP_VALIDATE + url, 5000, 5000);
        } catch (IOException e) {
            log.info("接口："+ Until.IF_APP_VALIDATE + url);
            e.printStackTrace();

        }
        if(StringUtils.isBlank(ifAppValidate)){
            log.info("App是否需要安全验证/第三方接口错误，获取失败");
            return coverMessage("201", "第三方接口错误，获取失败");
        }

        JSONObject jsb = JSONObject.parseObject(ifAppValidate);
        return coverMessage("200", "获取成功", jsb);

    }

    /**
     * 获取用户无查看权限的APP栏目列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getnoright",method = RequestMethod.POST)
    public Message getNoRightAppMenus(@RequestBody UserInfoFilter filter){
        String sysId = filter.getOpenId();
        UserInfo userInfo = userInfoService.getById(sysId);
        String userId = "";
        //用户Id
        if(ObjectUtils.isNotBlank(userInfo.getUserId())){
            userId = userInfo.getUserId();
        }
//        String url = "userid=" + userId;
        String url = "usercode=" + userId;
        String noright = null;
        try {
            noright = Until.HttpGET(Until.GET_NO_RIGHT + url, 5000, 5000);
        } catch (IOException e) {
            log.info("接口："+ Until.GET_NO_RIGHT + url);
            e.printStackTrace();
        }
        JSONObject jsonb = JSONObject.parseObject(noright);
        if (!"200".equals(jsonb.getString("returncode"))) {
            log.info("获取用户无查看权限的APP栏目列表/第三方接口错误，获取失败");
            return coverMessage("201", "第三方接口错误，获取失败");
        }
        String returndata = jsonb.getString("returndata");

//        if("200".equals(jsonb.getString("returncode"))){
//            if("false".equals(jsonb.getString("returndata"))){
//                return coverMessage("201", "该用户不是预制用户");
//            }
//        }

        if(StringUtils.isBlank(returndata)){
            log.info("获取用户无查看权限的APP栏目列表/第三方接口错误，获取失败");
            return coverMessage("201", "第三方接口错误，获取失败");
        }
        JSONArray jsb = JSONObject.parseArray(returndata);
        return coverMessage("200", "获取成功", jsb);
    }

    /**
     * 重置密码接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/chongzhi",method = RequestMethod.POST)
    public Message chongzhi(@RequestBody UserInfoFilter filter){
        String tel = filter.getTel();
        String password = filter.getPassWord();

        String url = "tel=" + tel + "&passwor=" + password;
        String chongzhi = null;
        try {
            chongzhi = Until.HttpGET(Until.RESET_PWD + url, 5000, 5000);
        } catch (IOException e) {
            log.info("接口："+ Until.RESET_PWD + url);
            e.printStackTrace();
        }
        if(StringUtils.isBlank(chongzhi)){
            log.info("重置密码接口/第三方接口错误，获取失败");
            return coverMessage("201", "第三方接口错误，获取失败");
        }
        JSONObject jsb = JSONObject.parseObject(chongzhi);
        return coverMessage("200", "重置密码成功", jsb);
    }

    /**
     * 激活接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/jihuo",method = RequestMethod.POST)
    public Message jihuo(@RequestBody UserInfoFilter filter){
        String tel = filter.getTel();               //电话号码
        String password = filter.getPassWord();     //密码
        String userid = "随机数";
        String url = "tel=" + tel + "&passwor=" + password +"&userid=" + userid;
        String jihuo = null;
        try {
            jihuo = Until.HttpGET(Until.JI_HUO + url, 5000, 5000);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("接口："+ Until.JI_HUO + url);
        }
        if(StringUtils.isBlank(jihuo)){
            log.info("激活接口/第三方接口错误，获取失败");
            return coverMessage("201", "第三方接口错误，获取失败");
        }
        JSONObject jsb = JSONObject.parseObject(jihuo);
        return coverMessage("200", "激活接口", jsb);
    }
}
